﻿using AutoMapper;
using ManagementApi.Models;
using ManagementApi.Models.Commander;
using ManagementApi.Mvc.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules;
using Modules.Commander;
using Modules.System.Security;
using Modules.System.User;
using PmSoft.ApplicationContext;
using PmSoft.Entity;
using PmSoft.Utilities;
using System.ComponentModel.DataAnnotations;

namespace ManagementApi.Controllers.AdminPannel
{
    /// <summary>
    /// 建设单位管理
    /// </summary>
    [Route("/api/v1/sysuser/commander")]
    [ApiController]
    [Authorize(Roles = "SysUser")]
    public class CommanderController : ControllerBase
    {
        private readonly CommanderService commanderService;
        private readonly IMapper mapper;
        private readonly SecurityService securityService;
        private readonly IApplicationContext applicationContext;

        public CommanderController(CommanderService commanderService, IMapper mapper, SecurityService securityService, IApplicationContext applicationContext)
        {
            this.commanderService = commanderService;
            this.mapper = mapper;
            this.securityService = securityService;
            this.applicationContext = applicationContext;
        }

        /// <summary>
        /// 指挥部分页
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("page")]
        public ApiResult<PageDto<CommanderDto>> GetCommanders([FromQuery] CommanderQuery query, int pageIndex = 1, int pageSize = 9999)
        {
            var data = commanderService.GetCommanders(query, pageIndex, pageSize);
            var dtos = data.AsPageDto(source => { return source.AsDto(applicationContext); });
            return ApiResult<PageDto<CommanderDto>>.Success(dtos);
        }

        /// <summary>
        /// 指挥部详情
        /// </summary>
        /// <param name="commanderId"></param>
        /// <returns></returns>
        [HttpGet("{commanderId}")]
        public ApiResult<CommanderDto> GetCommander(int commanderId)
        {
            var data = commanderService.GetCommanderInfo(commanderId);
            if (data == null)
                return ApiResult<CommanderDto>.Failure("指挥部数据不存在");
            return ApiResult<CommanderDto>.Success(data.AsDto(applicationContext));
        }

        /// <summary>
        /// 修改指挥部信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="from"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [PermissionCheck("corp:commander:edit")]
        public ApiResult Update(int id, [FromBody] CommanderEditForm from)
        {
            CommanderInfo? data = commanderService.GetCommanderInfo(id);
            if (data == null)
                return ApiResult.Failure("指挥部数据不存在");
            mapper.Map(from, data);
            commanderService.Update(data);
            return ApiResult.Success("修改成功");
        }

        /// <summary>
        /// 删除指挥部信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("{ids}")]
        [PermissionCheck("corp:commander:delete")]
        public ApiResult Deleted(string ids)
        {
            commanderService.Deletes(ValueUtility.ConvertToList<int>(ids));
            return ApiResult.Success("删除成功");
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut("resetPassword/{id}")]
        [PermissionCheck("corp:commander:resetpassword")]
        public ApiResult EditPassWord(int id, [Required(ErrorMessage = "密码不能为空")] string passWord)
        {
            CommanderInfo? data = commanderService.GetCommanderInfo(id);
            if (data == null)
                return ApiResult.Failure("指挥部数据不存在");
            SysSecurity? sysSecurity = securityService.GetSysSecurity(data.SecurityId);
            if (sysSecurity != null)
            {
                sysSecurity.Password = SecurityPasswordHelper.EncodePassword(passWord, UserPasswordFormat.SM4);
                securityService.Update(sysSecurity);
            }
            else
            {
                sysSecurity = new()
                {
                    Password = SecurityPasswordHelper.EncodePassword(passWord, UserPasswordFormat.SM4),
                    UserType = UserType.Commander,
                    IsAuth = false
                };
                securityService.Create(sysSecurity);
            }
            return ApiResult.Success("密码重置成功");
        }
    }
}